package com.example.a200peoject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    // 定义每个屏幕显示的应用程序数
    public static final int NUMBER_PER_SCREEN = 12;
    // 保存系统所有应用程序的List集合
    private List<DataItem> items = new ArrayList<>();
    private int screenNo = -1;
    private int screenCount = 0;
    private ViewSwitcher switcher;
    private LayoutInflater inflater;

    // 该BaseAdapter负责为每屏幕显示的View提供列表项
    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            // 如果当前是最后一屏，且应用程序的数量不足铺满一个屏幕
            if (screenNo == screenCount - 1 && items.size() % NUMBER_PER_SCREEN != 0) {
                return items.size() % NUMBER_PER_SCREEN;
            } else {
                return NUMBER_PER_SCREEN;
            }
        }

        @Override
        public DataItem getItem(int position) {
            // 根据screenNo计算第position个列表项的数据
            return items.get(screenNo * NUMBER_PER_SCREEN + position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 获取指定位置的视图
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = inflater.inflate(R.layout.lableicon, null);
                ImageView icon = view.findViewById(R.id.icon);
                TextView label = view.findViewById(R.id.label);
                viewHolder = new ViewHolder(icon, label);
                // 将viewHolder和view关联绑定，可以避免重复创建和查找视图
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.imageView.setImageDrawable(getItem(position).drawable);
            viewHolder.textView.setText(getItem(position).dataName);
            return view;
        }
    };

    // 代表应用程序的内部类
    public static class DataItem {
        // 应用程序名称
        String dataName;
        // 应用程序图标
        Drawable drawable;

        public DataItem(String dataName, Drawable drawable) {
            this.dataName = dataName;
            this.drawable = drawable;
        }
    }

    public static class ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolder(ImageView imageView, TextView textView) {
            this.imageView = imageView;
            this.textView = textView;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.constraintlayout_test);
        inflater = LayoutInflater.from(this);
        for (int i = 0; i < 40; i++) {
            String label = i + "";
            Drawable icon = getResources().getDrawable(R.mipmap.ic_launcher, null);
            items.add(new DataItem(label, icon));
        }
        // 计算应用程序所占的总屏幕数
        screenCount = items.size() / NUMBER_PER_SCREEN == 0 ?
                items.size() / NUMBER_PER_SCREEN : items.size() / NUMBER_PER_SCREEN + 1;
        switcher = findViewById(R.id.viewSwitcher);
        switcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return inflater.inflate(R.layout.slidelistview, null);
            }
        });

        // 页面加载的时候显示第一屏
        next(null);
    }
    public void next(View view) {
        if (screenNo < screenCount - 1) {
            screenNo++;
            // 为ViewSwitcher组件显示过程设置动画
            switcher.setInAnimation(this, R.anim.slide_in_right);
            // 设置隐藏过程的动画
            switcher.setOutAnimation(this, R.anim.slide_out_left);
            // 控制下一屏将要显示的GridView对应的Adapter
            ((GridView) switcher.getNextView()).setAdapter(adapter);
            // 显示下一屏
            switcher.showNext();
        }
    }

    public void pre(View view) {
        if (screenNo > 0) {
            screenNo--;
            // 为ViewSwitcher组件显示过程设置动画
            switcher.setInAnimation(this, R.anim.slide_in_right);
            // 设置隐藏过程的动画
            switcher.setOutAnimation(this, R.anim.slide_out_left);
            // 控制上一屏将要显示的GridView对应的Adapter
            ((GridView) switcher.getNextView()).setAdapter(adapter);
            // 显示上一屏
            switcher.showPrevious();
        }
    }
}